import { getKeyRouter, removeDateHmsStr, renderTableText, tableSorter, tooltipLength } from '@/utils';
import { Button, Tooltip } from 'antd';
import { AssetListManageAPI } from '../common/type';
import pageStyles from '@/common/pageLayout.less';
import { Fragment } from 'react';
import { EditOutlined, DeleteOutlined } from '@ant-design/icons';
import { tableMoreBtnConfig } from '@/utils/myutilConfig';
import MyIcon from '@/components/MyIcon';
import moment from 'moment';
import { history } from 'umi';
import { ColumnsRenderFun, DictionaryRenderFun, formatDecimal } from '@/common';
import { formatDecimalFour } from '../../Acommon';

/**操作按钮的显示*/
const Operation = (curRes: Record<string, any>, operateFnc: AssetListManageAPI.IMaterialTableActionType, record: Record<string, any>) => {
  let operationArray: any = [];

  /** 一个按钮都没有时展示*/
  const Disable = (
    <Fragment key={record?.id + '--Disable'}>
      <Button style={{ color: '#000' }} className="manageBtntTxtStyle" type={'link'} size={'small'} disabled>
        --
      </Button>
    </Fragment>
  );

  /** 更多按钮配置 */
  const otherBtnConfig: AssetListManageAPI.TableMoreBtnConfigApi[] = [
    {
      icon: <EditOutlined />,
      btnName: '变更',
      clickFnc: operateFnc,
      clickType: 'onEdit',
      isVisible: record.resultantAsset ? curRes?.['/assetInfo/resultant'] : curRes?.['/assetInfo/edit'],
    },
    {
      icon: <DeleteOutlined />,
      btnName: '删除',
      clickFnc: operateFnc,
      clickType: 'onCancel',
      isVisible: curRes?.['/assetInfo/resultantAssetRemove'] && record.resultantAsset,
    },
  ];
  const ohterBtn = tableMoreBtnConfig(otherBtnConfig, record);
  operationArray = ohterBtn;
  operationArray = operationArray?.filter((itemBtn: any) => itemBtn);
  return operationArray && operationArray?.length > 0 ? operationArray : [Disable];
};

/**
 * 表格信息 展示和操作
 * @returns
 */
const getTableColumns = (
  /** 资源权限参数 */
  curRes: Record<string, any>,
  /** 数据操作参数 */
  operateFnc: AssetListManageAPI.IMaterialTableActionType,
) => {
  return [
    {
      title: '资产编号',
      dataIndex: 'assetCode',
      key: 'assetCode',
      width: 180,
      fixed: 'left',
      visible: true,
      unDrag: true,
      sorter: (a: any, b: any) => (a.assetCode > b.assetCode ? 1 : -1),
      render: (text: string, record: Record<string, any>) => {
        return (
          <Tooltip title={tooltipLength(text, 50)}>
            <span className={pageStyles['over-flow-hidden']} style={{ width: 220, display: 'inline-block' }}>
              {record?.pId && <MyIcon type="icon-dian" isTheme={false} />}
              {text || '--'}
            </span>
          </Tooltip>
        );
      },
    },
    {
      title: '资产名称',
      dataIndex: 'assetName',
      key: 'assetName',
      width: 150,
      fixed: 'left',
      ellipsis: {
        showTitle: true,
      },
      visible: true,
      unDrag: true,
      sorter: (a: any, b: any) => (a.assetName > b.assetName ? 1 : -1),
      render: (text: string, record: Record<string, any>) => {
        return (
          <>
            <div className={`contract-managestyle-titleBox`}>
              {record?.resultantAsset ? (
                <Tooltip title={tooltipLength(text, 50)}>
                  <a
                    className={pageStyles['over-flow-hidden']}
                    onClick={() => {
                      history.push({
                        pathname: `/assetInfo/resultant/:${getKeyRouter()}${getKeyRouter()}`,
                        state: {
                          pageType: 'detail',
                          id: record.id,
                        },
                      });
                    }}
                  >
                    {text || '--'}
                  </a>
                  {/* <span>{text + '（主资产）' || '--'}</span> */}
                </Tooltip>
              ) : text ? (
                <Tooltip title={text}>
                  <a
                    className={pageStyles['over-flow-hidden']}
                    onClick={() => {
                      history.push({
                        pathname: `/assetInfo/detail/:${getKeyRouter()}${getKeyRouter()}`,
                        state: {
                          pageType: 'detail',
                          id: record.id,
                          returnPathName: history.location.pathname,
                        },
                      });
                    }}
                  >
                    {text || '--'}
                    {/* {text + (record?.pId ? '（配件）' : '') || '--'} */}
                  </a>
                </Tooltip>
              ) : (
                <>--</>
              )}
            </div>
            {record?.resultantAsset && (
              <div className={`contract-managestyle-titleBox-relateNumBox`} style={{ top: 0, right: 0, backgroundColor: '#ff0000' }}>
                <span className={`contract-managestyle-titleBox-countTextWithinTen`} style={{ right: 3 }}>
                  {'组'}
                </span>
              </div>
            )}
          </>
        );
      },
    },
    {
      title: '资产条码',
      dataIndex: 'assetBarCode',
      key: 'assetBarCode',
      width: 140,
      visible: false,
      sorter: (a: any, b: any) => (a.assetBarCode > b.assetBarCode ? 1 : -1),
      render: (text: string, record: Record<string, any>) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '资产分类',
      dataIndex: 'assetClassName',
      key: 'assetClassName',
      width: 140,
      ellipsis: {
        showTitle: true,
      },
      visible: true,
      sorter: (a: any, b: any) => (a.assetClassName > b.assetClassName ? 1 : -1),
      render: (text: string) => DictionaryRenderFun({ text }),
    },
    {
      title: '品牌',
      dataIndex: 'brandType',
      key: 'brandType',
      width: 110,
      visible: true,
      sorter: (a: any, b: any) => (a.brandType > b.brandType ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '规格型号',
      dataIndex: 'paramsInfo',
      key: 'paramsInfo',
      width: 160,
      visible: true,
      sorter: (a: any, b: any) => (a.paramsInfo > b.paramsInfo ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '状态',
      dataIndex: 'assetStateName',
      key: 'assetStateName',
      width: 120,
      visible: true,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetStateName'),
      render: (text: string) => DictionaryRenderFun({ text, isTooltip: true, isState: true }),
    },
    {
      title: '取得方式',
      dataIndex: 'obtainTypeName',
      key: 'obtainTypeName',
      width: 140,
      visible: false,
      sorter: (a: any, b: any) => (a.obtainTypeName > b.obtainTypeName ? 1 : -1),
      render: (text: number | null) => DictionaryRenderFun({ text, isState: false }),
    },
    {
      title: '取得日期',
      dataIndex: 'obtainDate',
      key: 'obtainDate',
      width: 140,
      visible: false,
      sorter: (a: any, b: any) => (a.obtainDate > b.obtainDate ? 1 : -1),
      render: (text: string) => {
        return <Tooltip title={text}>{text ? moment(text).format('YYYY-MM-DD') : '--'}</Tooltip>;
      },
    },
    {
      title: '入库日期',
      dataIndex: 'enterStoreTime',
      key: 'enterStoreTime',
      width: 160,
      visible: true,
      sorter: (a: any, b: any) => (a.enterStoreTime > b.enterStoreTime ? 1 : -1),
      render: (text: string) => removeDateHmsStr(text) || '--',
    },
    {
      title: '领用日期',
      dataIndex: 'useTime',
      key: 'useTime',
      width: 160,
      visible: false,
      sorter: (a: any, b: any) => (a.useTime > b.useTime ? 1 : -1),
      render: (text: string) => removeDateHmsStr(text) || '--',
    },
    {
      title: '预计归还日期',
      dataIndex: 'backTime',
      key: 'backTime',
      width: 160,
      visible: false,
      sorter: (a: any, b: any) => (a.backTime > b.backTime ? 1 : -1),
      render: (text: string) => removeDateHmsStr(text) || '--',
    },
    {
      title: '供应商',
      dataIndex: 'supplier',
      key: 'supplier',
      width: 150,
      visible: false,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetStateName'),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '数量',
      dataIndex: 'quantityOrArea',
      key: 'quantityOrArea',
      width: 100,
      visible: true,
      sorter: (a: any, b: any) => (a.quantityOrArea > b.quantityOrArea ? 1 : -1),
      render: (text: number | null) => formatDecimal(text),
    },
    {
      title: '计量单位',
      dataIndex: 'unitOfMeasurementName',
      key: 'unitOfMeasurementName',
      width: 140,
      visible: true,
      sorter: (a: any, b: any) => (a.unitOfMeasurementName > b.unitOfMeasurementName ? 1 : -1),
      render: (text: number | null) => DictionaryRenderFun({ text }),
    },
    {
      title: '资产原值（元）',
      dataIndex: 'totalValue',
      key: 'totalValue',
      width: 170,
      visible: true,
      sorter: (a: any, b: any) => (a.totalValue > b.totalValue ? 1 : -1),
      render: (text: number | null) => formatDecimalFour(text),
    },
    {
      title: '资产单价',
      dataIndex: 'assetValue',
      key: 'assetValue',
      width: 140,
      visible: false,
      sorter: (a: any, b: any) => (a.assetType > b.assetType ? 1 : -1),
      render: (text: number | null) => formatDecimalFour(text),
    },
    {
      title: '资产类型',
      dataIndex: 'assetTypeName',
      key: 'assetTypeName',
      width: 140,
      visible: false,
      sorter: (a: any, b: any) => (a.assetType > b.assetType ? 1 : -1),
      render: (text: number | null) => DictionaryRenderFun({ text, isTooltip: true }),
    },
    {
      title: '管理人单位',
      dataIndex: 'assetCompanyName',
      key: 'assetCompanyName',
      width: 160,
      visible: false,
      sorter: (a: any, b: any) => (a.assetCompanyID > b.assetCompanyID ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '管理人部门',
      dataIndex: 'assetDepartmentName',
      key: 'assetDepartmentName',
      width: 150,
      visible: false,
      sorter: (a: any, b: any) => (a.assetDepartmentID > b.assetDepartmentID ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '管理人岗位',
      dataIndex: 'assetPositionName',
      key: 'assetPositionName',
      width: 150,
      visible: false,
      sorter: (a: any, b: any) => (a.assetPositionID > b.assetPositionID ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '管理人',
      dataIndex: 'asseterName',
      key: 'asseterName',
      width: 140,
      visible: false,
      sorter: (a: any, b: any) => (a.userName > b.userName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '使用人单位',
      dataIndex: 'useCompanyName',
      key: 'useCompanyName',
      width: 150,
      visible: false,
      sorter: (a: any, b: any) => (a.useCompanyID > b.useCompanyID ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '使用人部门',
      dataIndex: 'useDepartmentName',
      key: 'useDepartmentName',
      width: 150,
      visible: false,
      sorter: (a: any, b: any) => (a.useDepartmentID > b.useDepartmentID ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '使用人岗位',
      dataIndex: 'usePositionName',
      key: 'usePositionName',
      width: 150,
      visible: false,
      sorter: (a: any, b: any) => (a.usePositionID > b.usePositionID ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '使用人',
      dataIndex: 'userName',
      key: 'userName',
      width: 140,
      visible: false,
      sorter: (a: any, b: any) => (a.userID > b.userID ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '资产来源',
      dataIndex: 'sourceTypeName',
      key: 'sourceTypeName',
      width: 140,
      visible: false,
      sorter: (a: any, b: any) => (a.sourceType > b.sourceType ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '存放区域',
      dataIndex: 'locationName',
      key: 'locationName',
      width: 140,
      visible: false,
      sorter: (a: any, b: any) => (a.locationName > b.locationName ? 1 : -1),
      render: (text: number | null) => DictionaryRenderFun({ text, isState: false }),
    },
    {
      title: '详细地点',
      dataIndex: 'locationInfo',
      key: 'locationInfo',
      width: 140,
      visible: false,
      sorter: (a: any, b: any) => (a.locationInfo > b.locationInfo ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '合同编号',
      dataIndex: 'contractCode',
      key: 'contractCode',
      width: 140,
      visible: false,
      sorter: (a: any, b: any) => (a.contractCode > b.contractCode ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '财务入账状态',
      dataIndex: 'entryStateName',
      key: 'entryStateName',
      width: 160,
      visible: false,
      sorter: (a: any, b: any) => (a.entryStateName > b.entryStateName ? 1 : -1),
      render: (text: number | null) => DictionaryRenderFun({ text, isState: false }),
    },
    {
      title: '预算一体化编号',
      dataIndex: 'inventoryNumber',
      key: 'inventoryNumber',
      width: 160,
      visible: false,
      sorter: (a: any, b: any) => (a.inventoryNumber > b.inventoryNumber ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    // {
    //   title: '操作',
    //   dataIndex: 'operation',
    //   key: 'operation',
    //   width: 150,
    //   visible: true,
    //   unDrag: true,
    //   fixed: 'right',
    //   render: (text: any, record: any) => {
    //     return (
    //       <div
    //         style={{
    //           display: 'flex',
    //         }}
    //       >
    //         {Operation(curRes, operateFnc, record)}
    //       </div>
    //     );
    //   },
    // },
  ];
};

export { getTableColumns };
